unit TPS_Print;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Shellapi, Filectrl,
  Dialogs, StdCtrls, ActiveX,ExtCtrls, Setting, ZAbstractTable, ZDataset, DB,
  ZAbstractRODataset, ZAbstractDataset, ZConnection, Unit_External, frxClass,
  frxDBSet, frxExportPDF, frxExportXLS, JvThread, JvThreadDialog, Math,
  JvComponentBase, frxpngimage;

type
  TTPS_Print_Form = class(TForm)
    cbkec: TComboBox;
    cbkeldes: TComboBox;
    xlsbutton: TButton;
    cbtps: TComboBox;
    cbkabkot: TComboBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    ZTable2: TZTable;
    ZQuery2: TZQuery;
    frxDBDataset3: TfrxDBDataset;
    frxReport2: TfrxReport;
    pdfbutton: TButton;
    printbutton: TButton;
    Button4: TButton;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    RadioButton5: TRadioButton;
    RadioButton6: TRadioButton;
    RadioButton7: TRadioButton;
    RadioButton8: TRadioButton;
    frxReport1: TfrxReport;
    ZQuery3: TZQuery;
    frxPDFExport1: TfrxPDFExport;
    frxXLSExport1: TfrxXLSExport;
    JvThread1: TJvThread;
    JvThreadSimpleDialog1: TJvThreadSimpleDialog;
    JvThread2: TJvThread;
    JvThreadSimpleDialog2: TJvThreadSimpleDialog;
    cbtps2: TComboBox;
    Label6: TLabel;
    Image2: TImage;
    Image1: TImage;
    ZTable3: TZTable;
    printkabkot: TButton;
    pdfkabkot: TButton;
    ZQuery4: TZQuery;
    tblAntara: TZTable;
    Memo1: TMemo;
    Label7: TLabel;
    cbUrut: TComboBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure cbkabkotChange(Sender: TObject);
    procedure cbkecChange(Sender: TObject);
    procedure cbkeldesChange(Sender: TObject);
    procedure xlsbuttonClick(Sender: TObject);
    procedure cbtpsChange(Sender: TObject);
    procedure printTPS(dir:string;typeext:string;model:integer;kodedesa:string;kodetps:string;textkabkot:string;textkec:string;textdes:string; texttps:string);
    procedure printTPSperKec(dir:string;typeext:string;model:integer;kodekec:string;kodetps:string;textkabkot:string;textkec:string;textdes:string;texttps:string);
    procedure cbformatChange(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton3Click(Sender: TObject);
    procedure RadioButton4Click(Sender: TObject);
    procedure RadioButton5Click(Sender: TObject);
    procedure RadioButton6Click(Sender: TObject);
    procedure RadioButton7Click(Sender: TObject);
    procedure RadioButton8Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure exportpdf2(dir:string;print:string;model:integer;table:TZtable; query:TZQuery;query2:TZQuery;ikota,ikec,namakot, namakec:string);
    procedure ShowFolder(strFolder: string);
    procedure cetakA1(Sender: TObject);
    procedure cetakA(Sender: TObject);
    procedure cetakA5(Sender: TObject);
    procedure prosesketganda(sddes:string);
    procedure JvThread1Execute(Sender: TObject; Params: Pointer);
    procedure JvThread2Execute(Sender: TObject; Params: Pointer);
    function gettabel(id:string;tabelid:tstringlist;tabelname:tstringlist):string;
    procedure cbtpsSelect(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; kode_login: String);             overload;
  end;

var
  TPS_Print_Form: TTPS_Print_Form;
  sformat, skabkot, skec, skeldes, stps:string;
  choose_format:integer;
  kodekabkot,kodeKec:string;
  t_dir,t_textkabkot, t_namadesa,t_namakec,t_namakot,t_namatps,t_textkec, t_textdes, t_texttps:string;
  t_model:integer;
  pemilihan:string;



implementation

{$R *.dfm}

constructor TTPS_Print_Form.Create(AOwner: TComponent; kode_login:String);
  begin
    if(length(kode_login)>2)then
    begin
      kodekabkot:=copy(kode_login,1,2);
      kodekec:=copy(kode_login,3,3);
    end
    else
    begin
      kodekabkot:=kode_login;
      kodekec:='';
    end;
    inherited Create(AOwner);
  end;

procedure TTPS_Print_Form.ShowFolder(strFolder: string);
begin
  ShellExecute(Application.Handle,
    PChar('explore'),
    PChar(strFolder),
    nil,
    nil,
    SW_SHOWNORMAL);
end;

procedure TTPS_Print_Form.exportpdf2(dir:string;print:string;model:integer;table:TZtable; query:TZQuery;query2:TZQuery;ikota,ikec,namakot, namakec:string);
var counter:integer;i:integer;jumlahtps, jumlahlaki,jumlahperempuan, jumlahtambahan,kode:string;
  listquery:tstringlist;
begin
  listquery:=tstringlist.Create;
  counter:=1;
  zquery3.Active:=false;
  zquery3.sql.Clear;
  ZQuery3.SQL.Text:='TRUNCATE TABLE rekap ';
  ZQuery3.ExecSQL;
  ZQuery3.Active:=false;
  ZQuery3.SQL.Clear;

  with table do
  begin
    active:=false;
    if(model=5) then
    begin
      tablename:='desa';
      filter:='KODE_KECAMATAN='+QuotedStr(ikec);
      filtered:=true;

    end
    else if(model=6)then
    begin
      tablename:='kecamatan';
      filter:='KODE_KABKOT='+QuotedStr(ikota);
      filtered:=true;
    end
    else if(model=7)then
    begin
      tablename:='kabkot';
      filtered:=false;
    end;
    active:=true;
    first;
    while (not eof) do
    begin
      query.Active:=false;
      query.sql.Clear;
      if(model=5)then
      query.sql.Text:='select * from penyimpan where NAMA LIKE '+QuotedStr('pemilih_'+FieldByName('KODE_DESA').AsString+'%')
      else if(model=6)then
      query.sql.Text:='select * from penyimpan where NAMA LIKE '+QuotedStr('pemilih_'+FieldByName('KODE_KECAMATAN').AsString+'%')
      else if(model=7)then
      query.sql.Text:='select * from penyimpan where NAMA LIKE '+QuotedStr('pemilih_'+FieldByName('KODE').AsString+'%');
      //showmessage('HASIL:'+ikota);

      query.Active:=true;
      query.First;
      jumlahtps:='0';
      jumlahlaki:='0';
      jumlahperempuan:='0';
      jumlahtambahan:='0';
      if(query.RecordCount>0)then
      begin
        while (not query.Eof) do
        begin
          kode:=query.FieldByName('NAMA').AsString;

          query2.Active:=false;
          query2.sql.Clear;
          query2.sql.Text:='select ID_TPS from daftar_tps where KODE_DESA = '+quotedStr(copy(kode,9,8));
          query2.Active:=true;
          jumlahtps:=inttostr(strtoint(jumlahtps)+query2.RecordCount);

          query2.Active:=false;
          query2.sql.Clear;
          query2.sql.Text:='select count(ID_USER) as jumlah from '+kode+' where STATUS1<>'+QuotedStr('0')+' and STATUS1<>'+QuotedStr('2')+' and STATUS2 = ''0''';
          query2.Active:=true;
          jumlahtambahan:=inttostr(strtoint(jumlahtambahan)+strtoint(query2.FieldByName('jumlah').AsString));

          query2.Active:=false;
          query2.sql.Clear;
          query2.sql.Text:='select count(ID_USER) as jumlah from '+kode+' where KELAMIN="L" and TPS>0 and STATUS2 = ''0''';
          query2.Active:=true;
          jumlahlaki:=inttostr(strtoint(jumlahlaki)+strtoint(query2.FieldByName('jumlah').AsString));

          query2.Active:=false;
          query2.sql.Clear;
          query2.sql.Text:='select count(ID_USER) as jumlah from '+kode+' where KELAMIN="P" and TPS>0 and STATUS2 = ''0''';
          query2.Active:=true;
          jumlahperempuan:=inttostr(strtoint(jumlahperempuan)+strtoint(query2.FieldByName('jumlah').AsString));
          query.next;
        end;
        listquery.Add('('+inttostr(counter)+','+QuotedStr(FieldByName('TAMPIL').AsString)+','+jumlahlaki+','+jumlahperempuan+',' +inttostr(strtoint(jumlahlaki)+strtoint(jumlahperempuan))+','+jumlahtps+','+jumlahtambahan+')');

        counter:=counter+1;
        i:=i+1;
        next;
      end
      else
      begin
        listquery.Add('('+inttostr(counter)+','+QuotedStr(FieldByName('TAMPIL').AsString)+',0,0,0,0,0)');
        counter:=counter+1;
        i:=i+1;
        next;

      end;
    end;
  end;

  ZQuery3.SQL.Text:='insert into rekap (URUT, NAMA, JUMLAH_L, JUMLAH_P, TOTAL, JUMLAH_TPS, KETERANGAN) VALUES ';
  for I := 0 to listquery.Count - 1 do
    begin
      ZQuery3.SQL.Text:=ZQuery3.SQL.Text+listquery[I];
      if(I+1<>listquery.Count)then
        ZQuery3.SQL.Text:=ZQuery3.SQL.Text+',';
    end;

  zquery3.ExecSQL;
  zquery3.Active:=false;
  zquery3.sql.Clear;
  zquery3.sql.Text:='select * from rekap';
  zquery3.Active:=true;
  zquery3.first;

    frxReport1.Variables['MODEL'] :=QuotedStr('MODEL A'+inttostr(model)+' - KWK.KPU');
  // header tengah
  frxReport1.Variables['JUDUL1']:=QuotedStr('REKAPITULASI JUMLAH PEMILIH TERDAFTAR');
  frxReport1.Variables['JUDUL2']:=QuotedStr('PEMILIHAN GUBERNUR DAN WAKIL GUBERNUR JAWA BARAT TAHUN 2013');
  if(model=5)then
  begin
    frxReport1.Variables['JUDUL3']:=QuotedStr('OLEH PANITIA PEMILIHAN KECAMATAN');
    frxReport1.Variables['SUB1']:=QuotedStr('PANITIA PEMILIHAN KECAMATAN');
    frxReport1.Variables['SUB2']:=QuotedStr('KABUPATEN/KOTA');
    frxReport1.Variables['SUB3']:=QuotedStr('PROVINSI');
    frxReport1.Variables['SUB_CONTENT1']:=QuotedStr(': '+namakec);
    frxReport1.Variables['SUB_CONTENT2']:=QuotedStr(': '+namakot);
    frxReport1.Variables['SUB_CONTENT3']:=QuotedStr( ': JAWA BARAT');
    frxReport1.Variables['WILAYAH']:=QuotedStr('DESA/KELURAHAN');
    frxReport1.Variables['PANITIA']:=QuotedStr('PANITIA PEMILIHAN KECAMATAN');
    frxReport1.Variables['KEPANITIAAN']:=QuotedStr('PPK');
  end
  else if(model=6)then
  begin
    frxReport1.Variables['JUDUL3']:=QuotedStr('OLEH KPU KABUPATEN/KOTA');
    frxReport1.Variables['SUB1']:=QuotedStr('KPU KABUPATEN/KOTA 1)');
    frxReport1.Variables['SUB2']:=QuotedStr('PROVINSI');
    frxReport1.Variables['SUB3']:=QuotedStr('');
    frxReport1.Variables['SUB_CONTENT1']:=QuotedStr(': '+namakot);
    frxReport1.Variables['SUB_CONTENT2']:=QuotedStr( ': JAWA BARAT');
    frxReport1.Variables['SUB_CONTENT3']:=QuotedStr('');
    frxReport1.Variables['WILAYAH']:=QuotedStr('KECAMATAN');
    frxReport1.Variables['PANITIA']:=QuotedStr('KOMISI PEMILIHAN UMUM KABUPATEN/KOTA');
    frxReport1.Variables['KEPANITIAAN']:=QuotedStr('KPU KAB/KOTA');
  end
  else if(model=7)then
  begin
    frxReport1.Variables['JUDUL3']:='OLEH KPU PROVINSI';
    frxReport1.Variables['SUB1']:=QuotedStr('KPU PROVINSI');
    frxReport1.Variables['SUB2']:=QuotedStr('');
    frxReport1.Variables['SUB3']:=QuotedStr('');
    frxReport1.Variables['SUB_CONTENT1']:=QuotedStr(': JAWA BARAT');
    frxReport1.Variables['SUB_CONTENT2']:=QuotedStr( '');
    frxReport1.Variables['SUB_CONTENT3']:=QuotedStr('');
    frxReport1.Variables['WILAYAH']:=QuotedStr('KABUPATEN/KOTA');
    frxReport1.Variables['PANITIA']:=QuotedStr('KOMISI PEMILIHAN UMUM PROVINSI JAWA BARAT');
    frxReport1.Variables['KEPANITIAAN']:=QuotedStr('KPU PROVINSI');
  end;
  frxReport1.Variables['JUDUL2'] :=QuotedStr(pemilihan);
  frxReport1.Variables['JUDUL3'] :=QuotedStr('TAHUN 2013');

  frxReport1.Variables['HALAMAN'] :=QuotedStr('.........');
  frxReport1.Variables['KETUA'] :=QuotedStr('.........');
  frxReport1.Variables['ANGGOTA1'] :=QuotedStr('.........');
  frxReport1.Variables['ANGGOTA2'] :=QuotedStr('.........');
  frxReport1.Variables['ANGGOTA3'] :=QuotedStr('.........');
  frxReport1.Variables['ANGGOTA4'] :=QuotedStr('.........');
  // Save the Workbook
  if not DirectoryExists(dir+'\Excel\') then CreateDir(dir+'\Excel\');
  if not DirectoryExists(dir+'\Pdf\') then CreateDir(dir+'\Pdf\');
  if(model=5)then
  begin
    with ztable2 do
      begin
        active:=false;
        tablename:='petugas';
        filter:='KODE_KECAMATAN='+QuotedStr(copy(kode,9,5))+' and POSISI=3';
        filtered:=true;
        active:=true;
        first;
        while (not eof) do
        begin
          if(fieldbyname('JABATAN').asstring = '1') then
          begin
            frxReport1.Variables['KETUA'] :=QuotedStr(fieldbyname('NAMA').asstring);
          end;
          if(fieldbyname('JABATAN').asstring='2')then
          begin
            if(frxReport1.Variables['ANGGOTA1'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA1'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA2'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA2'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA3'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA3'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA4'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA4'] :=QuotedStr(fieldbyname('NAMA').asstring)
          end;
          next;
        end;
      end;
    if not DirectoryExists(dir+'\Excel\'+namakot) then CreateDir(dir+'\Excel\'+namakot);
    if not DirectoryExists(dir+'\Excel\'+namakot+'\'+namakec) then CreateDir(dir+'\Excel\'+namakot+'\'+namakec);
    if not DirectoryExists(dir+'\Pdf\'+namakot) then CreateDir(dir+'\Pdf\'+namakot);
    if not DirectoryExists(dir+'\Pdf\'+namakot+'\'+namakec) then CreateDir(dir+'\Pdf\'+namakot+'\'+namakec);
  end
  else if(model=6)then
  begin
    with ztable2 do
    begin
        active:=false;
        tablename:='petugas';
        filter:='KODE_KABKOT='+QuotedStr(copy(kode,9,2))+' and POSISI=2';
        filtered:=true;
        active:=true;
        first;
        while (not eof) do
        begin
          if(fieldbyname('JABATAN').asstring = '1') then
          begin
            frxReport1.Variables['KETUA'] :=QuotedStr(fieldbyname('NAMA').asstring);
          end;
          if(fieldbyname('JABATAN').asstring='2')then
          begin
            if(frxReport1.Variables['ANGGOTA1'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA1'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA2'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA2'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA3'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA3'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA4'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA4'] :=QuotedStr(fieldbyname('NAMA').asstring)
          end;
          next;
        end;
      end;
    if not DirectoryExists(dir+'\Excel\'+namakot) then CreateDir(dir+'\Excel\'+namakot);
    if not DirectoryExists(dir+'\Pdf\'+namakot) then CreateDir(dir+'\Pdf\'+namakot);
  end else
  begin
    with ztable2 do
    begin
        active:=false;
        tablename:='petugas';
        filter:='POSISI=1';
        filtered:=true;
        active:=true;
        first;
        while (not eof) do
        begin
          if(fieldbyname('JABATAN').asstring = '1') then
          begin
            frxReport1.Variables['KETUA'] :=QuotedStr(fieldbyname('NAMA').asstring);
          end;
          if(fieldbyname('JABATAN').asstring='2')then
          begin
            if(frxReport1.Variables['ANGGOTA1'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA1'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA2'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA2'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA3'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA3'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport1.Variables['ANGGOTA4'] =QuotedStr('.........'))then
              frxReport1.Variables['ANGGOTA4'] :=QuotedStr(fieldbyname('NAMA').asstring)
          end;
          next;
        end;
      end;
  end;
  frxDBDataset3.DataSet:=zquery3;
  if(print='pdf')then
      begin
        frxReport1.PrintOptions.ShowDialog:=false;
        frxReport1.PrepareReport;
        frxPDFExport1.ShowDialog:=false;
        if(model=5)then
        begin
          frxPDFExport1.FileName:=dir+'\Pdf\'+namakot+'\'+namakec+'-FormA5.pdf';
          ShowFolder(dir+'\Pdf\'+namakot+'\');
        end
        else if(model=6)then
        begin
          frxPDFExport1.FileName:=dir+'\Pdf\'+namakot+'-FormA6.pdf';
          ShowFolder(dir+'\Pdf\');
        end
        else
        begin
          frxPDFExport1.FileName:=dir+'\Pdf\FormA7.pdf';
          ShowFolder(dir+'\Pdf\');
        end;
        frxPDFExport1.OpenAfterExport:=false;
        frxReport1.Export(frxPDFExport1);
      end
  else if(print='xls')then begin
    frxReport1.PrintOptions.ShowDialog:=false;
    frxReport1.PrepareReport;
    frxXLSExport1.ShowDialog:=false;
    if(model=5)then
      frxXLSExport1.FileName:=dir+'\Excel\'+namakot+'\'+namakec+'-FormA5.xls'
    else if(model=6)then
      frxXLSExport1.FileName:=dir+'\Excel\'+namakot+'-FormA6.xls'
    else
      frxXLSExport1.FileName:=dir+'\Excel\FormA7.xls';
    //frxXLSExport1.FileName:='asdf.xls';
    //frxXLSExport1.OpenExcelAfterExport:=true;
    frxReport1.Export(frxXLSExport1);
  end
  else begin
    //frxReport1.preview
    //frxReport1.PrintOptions.ShowDialog:=true;
    frxReport1.ShowReport(true);
    //frxReport1.ReportOptions.Name:='something.pdf';
    //if frxReport1.PrepareReport(true) then
    //  frxReport1.Print;
  end;

end;
procedure TTPS_Print_Form.xlsbuttonClick(Sender: TObject);
var ext : ExternalFile;

begin
  //showmessage(inttostr(choose_format));
  //showmessage(skec);
  //petugas kecamatan
  if(kodekec <> '') then begin

  if(choose_format>=5)then
  begin
    cetakA5(Sender);
  end
  else //harus mengisi kode desa
  if(cbkeldes.ItemIndex > 0) then begin
    if(choose_format=0)then
    begin
      cetakA(Sender);
    end
    else
      begin
      if((cbtps.ItemIndex=0)or(cbtps2.ItemIndex=0)or(cbtps.ItemIndex>cbtps2.ItemIndex))then
        showmessage('Maaf, periksa pilihan TPS Anda')
      else
        cetakA1(Sender);
      end;
    end else
      showmessage('Silakan pilih kel/desa terlebih dahulu');
  end else //petugas kabkot
  begin
    if(choose_format>=5)then
    begin
      cetakA5(Sender);
    end
    else if(choose_format=0)then
    begin
      cetakA(Sender);
    end
    else
    begin
      cetakA1(Sender);
    end;
  end;
end;

function TTPS_Print_Form.gettabel(id:string;tabelid:tstringlist;tabelname:tstringlist):string;
var i:integer;
begin
//
  result:='';
  for i:=0 to tabelid.count-1 do
  begin
    if(tabelid[i]=id)then
      result:=copy(tabelname[i],9,length(tabelname[i]));
  end;
end;

procedure TTPS_Print_Form.prosesketganda(sddes:string);
var idpenyimpan,pid1,pid2,id1,id2, tabel1,tabel2,levelname:string;
    splitter:tstringlist;
    tabelid,tabelname:tstringlist;

  level:integer;
begin
//
    //showmessage(skeldes);
    tabelid:=tstringlist.create;
    tabelname:=tstringlist.create;
    with ztable1 do
    begin
      active:=false;
      tablename:='penyimpan';
      active:=true;
      first;
      while (not eof) do
      begin
        tabelid.Add(fieldbyname('ID').asstring);
        tabelname.Add(fieldbyname('NAMA').asstring);
        next;
      end;
    end;
    with ztable1 do
    begin
      active:=false;
      tablename:='penyimpan';
      filter:='NAMA='+quotedstr('pemilih_'+sddes);
      filtered:=true;
      active:=true;
      first;
      idpenyimpan:=fieldbyname('ID').asstring;
    end;
    ztable1.active:=false;
    ztable1.tablename:='pemilih_'+sddes;
    ztable1.filtered:=false;
    ztable1.active:=true;
    ztable1.first;
    while (not ztable1.eof) do
    begin
      if(ztable1.fieldbyname('GANDA').asstring<>'0')then
      begin
        level:=1; // se kelurahan
        with zquery1 do
        begin
          active:=false;
          sql.clear;
          sql.text:='select * from data_ganda where ID_USER1='+quotedstr(idpenyimpan+'-'+ztable1.fieldbyname('ID_USER').asstring)+' OR ID_USER2='+quotedstr(idpenyimpan+'-'+ztable1.fieldbyname('ID_USER').asstring);
          active:=true;
          first;
          while (not eof) do
          begin
            splitter:=tstringlist.create;
            ExtractStrings(['-'], [], PChar(fieldbyname('ID_USER1').asstring), splitter);
            pid1:=splitter[0];
            id1:=splitter[1];
            splitter:=tstringlist.create;
            ExtractStrings(['-'], [], PChar(fieldbyname('ID_USER2').asstring), splitter);
            pid2:=splitter[0];
            id2:=splitter[1];
            tabel1:=gettabel(pid1, tabelid, tabelname);
            tabel2:=gettabel(pid2, tabelid, tabelname);

            if(pid1<>pid2)then
            begin
              if(copy(tabel1,1,2)<>copy(tabel2,1,2))then // beda kabupaten
              begin
                if(level<4) then level:=4;
              end
              else if(copy(tabel1,3,3)<>copy(tabel2,3,3))then // beda kecamatan
              begin
                if(level<3) then level:=3;
              end
              else if(copy(tabel1,3,3)=copy(tabel2,3,3))then // beda kelurahan
              begin
                if(level<2) then level:=2;
              end;
              //showmessage(tabel1+' dengan '+tabel2+' level '+inttostr(level));

            end;
            //showmessage(pid1+'-'+id1+' dengan '+pid2+'-'+id2);
            if(level=1)then levelname:='Ganda Satu Desa'
            else if(level=2)then levelname:='Ganda Antar Desa'
            else if(level=3)then levelname:='Ganda Antar Kecamatan'
            else if(level=4)then levelname:='Ganda Antar Kab/Kota';
            zquery4.active:=false;
            zquery4.sql.clear;
            zquery4.sql.Text:='update pemilih_'+sddes+' set KET='+quotedstr(levelname)+' where ID_USER='+quotedstr(ztable1.fieldbyname('ID_USER').asstring);
            zquery4.execsql;
            next;
          end;
        end;
      end;
      ztable1.next;
    end;

end;
procedure TTPS_Print_Form.cetakA(Sender: TObject);
var ext : ExternalFile;
  namadesa,namakec,namakot, namatps:string;
  dir:string;
  lbg_jabar, lbg_kabkot : Tfrxpictureview;
begin
      pemilihan:='PEMILIHAN GUBERNUR DAN WAKIL GUBERNUR JAWA BARAT';
//      showmessage('test');
//      showmessage('test');
      if(cbkabkot.ItemIndex=0)then
      begin
        //showmessage('masuk');
        ZQuery2.active:=false;
        ZQuery2.sql.Clear;
        ZQuery2.sql.Text:='select * from penyimpan';
        zquery2.Active:=true;
        zquery2.First;
      end
      else if(cbkec.ItemIndex=0)then
      begin
        ZQuery2.active:=false;
        ZQuery2.sql.Clear;
        ZQuery2.sql.Text:='select * from penyimpan where NAMA LIKE "pemilih_'+skabkot+'%"';
        zquery2.Active:=true;
        zquery2.First;
      end
      else if(cbkeldes.ItemIndex=0)then
      begin
        ZQuery2.active:=false;
        ZQuery2.sql.Clear;
        ZQuery2.sql.Text:='select * from penyimpan where NAMA LIKE "pemilih_'+skec+'%"';
        zquery2.Active:=true;
        zquery2.First;
      end
      else
      begin
        ZQuery2.active:=false;
        ZQuery2.sql.Clear;
        ZQuery2.sql.Text:='select * from penyimpan where NAMA LIKE '+quotedstr('pemilih_'+skeldes);
        //showmessage(skeldes);
        zquery2.Active:=true;
        zquery2.First;
      end;
      if(Zquery2.recordcount>0)then
      begin
        if((Sender<>printbutton) and (sender <>printkabkot))then
          SelectDirectory('Select a directory', 'C:\, D:\, E:\', dir);
          //SelectDirectory(dir, [sdAllowCreate, sdPerformCreate, sdPrompt],1000);
          if((sender=xlsbutton)and(dir<>'')) then
          begin
            t_namadesa:=namadesa;
            t_namakec:=namakec;
            t_namakot:=namakot;
            t_namatps:=namatps;
            t_dir:=dir;
            JvThread2.ThreadDialog := JvThreadSimpleDialog2;
            JvThread2.Execute(Self);
          end
          else
          begin
            //showmessage('record : '+inttostr(zquery2.recordcount));

            

            while (not zquery2.eof) do
            begin
              with ztable1 do
              begin
                active:=false;
                tablename:='desa';
                filter:='KODE_DESA='+QuotedStr(copy(ZQuery2.FieldByName('NAMA').AsString,9,8));
                filtered:=true;
                active:=true;
                first;
                namadesa:=FieldByName('TAMPIL').AsString;
                namakec:=FieldByName('KODE_KECAMATAN').AsString; // hanya sementara

                active:=false;
                tablename:='kecamatan';
                filter:='KODE_KECAMATAN='+QuotedStr(namakec);
                filtered:=true;
                active:=true;
                first;
                namakec:=FieldByName('TAMPIL').AsString;
                namakot:=FieldByName('KODE_KABKOT').AsString; // hanya sementara

                active:=false;
                tablename:='kabkot';
                filter:='KODE='+QuotedStr(namakot);
                filtered:=true;
                active:=true;
                first;
                namakot:=FieldByName('TAMPIL').AsString;
              end;



              //showmessage('proses '+copy(ZQuery2.FieldByName('NAMA').AsString,9,8));
              prosesketganda(copy(ZQuery2.FieldByName('NAMA').AsString,9,8));
              //munculkan lambang jabar, hilangkan lambang lain
              //lbg jabar
              lbg_jabar := Tfrxpictureview(frxReport2.FindObject('Picture2'));
              lbg_jabar.visible:=true;
              //lbgkotacirebon
              lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotcirebon'));
              lbg_kabkot.Visible:=false;
              //lbgkabsumedang
              lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kabsumedang'));
              lbg_kabkot.Visible:=false;
              //lbgkotasukabumi
              lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotsukabumi'));
              lbg_kabkot.Visible:=false;
              if((sender=xlsbutton)and(dir<>'')) then
                printTPS(dir,'xls',choose_format, copy(ZQuery2.FieldByName('NAMA').AsString,9,8), '',
              namakot, namakec, namadesa, '')
              else if((sender=pdfbutton)and(dir<>''))then
                printTPS(dir,'pdf',choose_format, copy(ZQuery2.FieldByName('NAMA').AsString,9,8), '',
              namakot, namakec, namadesa, '')
              else if(sender=printbutton)then
                printTPS(dir,'print',choose_format, copy(ZQuery2.FieldByName('NAMA').AsString,9,8), '',
              namakot, namakec, namadesa, '')
              else if(sender=pdfkabkot) then begin
                if (kodekabkot = '72') then begin //kota sukabumi
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotsukabumi'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA SUKABUMI';
                end
                else if (kodekabkot = '74') then begin //kota cirebon
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotcirebon'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA CIREBON';
                end
                else if (kodekabkot = '11') then begin //kabsumedang
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kabsumedang'));
                  pemilihan:='PEMILIHAN BUPATI DAN WAKIL BUPATI SUMEDANG';
                end;
                lbg_jabar := Tfrxpictureview(frxReport2.FindObject('Picture2'));
                lbg_jabar.visible:=false;
                lbg_kabkot.Visible:=true;
                printTPS(dir,'pdf',choose_format, copy(ZQuery2.FieldByName('NAMA').AsString,9,8), '',
                namakot, namakec, namadesa, '');
              end
              else if(sender=printkabkot) then begin
                if (kodekabkot = '72') then begin //kota sukabumi
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotsukabumi'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA SUKABUMI';
                end
                else if (kodekabkot = '74') then begin //kota cirebon
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotcirebon'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA CIREBON';
                end
                else if (kodekabkot = '11') then begin //kabsumedang
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kabsumedang'));
                  pemilihan:='PEMILIHAN BUPATI DAN WAKIL BUPATI SUMEDANG';
                end;
                lbg_jabar := Tfrxpictureview(frxReport2.FindObject('Picture2'));
                lbg_jabar.visible:=false;
                lbg_kabkot.Visible:=true;
                printTPS(dir,'print',choose_format, copy(ZQuery2.FieldByName('NAMA').AsString,9,8), '',
                namakot, namakec, namadesa, '');
              end;
              zquery2.Next;
            end;
          end;
          if((zquery2.recordcount>0)and(dir<>''))then
          begin
            if(sender=xlsbutton)then
              ShowFolder(dir)
            else if(sender=pdfbutton)then
              ShowFolder(dir);
          end;
          //else Showmessage('Data pada wilayah tersebut belum terisi');

      end
      else Showmessage('Data pada wilayah tersebut belum terisi');
end;

procedure TTPS_Print_Form.cetakA5(Sender: TObject);
var ext : ExternalFile;
  namadesa,namakec,namakot, namatps:string;
  valid:boolean;
  dir:string;
  lbg_jabar, lbg_kabkot : Tfrxpictureview;
begin
  pemilihan := 'PEMILIHAN GUBERNUR DAN WAKIL GUBERNUR JAWA BARAT';
  if(((choose_format=5) and (skec<>'') and(skabkot<>'')) or((choose_format=6) and (skabkot<>'')) or (choose_format=7))then
    begin
      if((Sender<>printbutton) and (Sender<>printkabkot))then
        SelectDirectory('Select a directory', 'C:\, D:\, E:\', dir);

      //munculkan lambang jabar, hilangkan lambang lain
      //lbg jabar
      lbg_jabar := Tfrxpictureview(frxReport1.FindObject('Picture2'));
      lbg_jabar.visible:=true;
      //lbgkotacirebon
      lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kotcirebon'));
      lbg_kabkot.Visible:=false;
      //lbgkabsumedang
      lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kabsumedang'));
      lbg_kabkot.Visible:=false;
      //lbgkotasukabumi
      lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kotsukabumi'));
      lbg_kabkot.Visible:=false;

      ext := ExternalFile.Create;
      if(sender=xlsbutton) then
        ext.exportExcelmodel2(dir, choose_format,ztable1,ZQuery1, ZQuery2, skabkot, skec, cbkabkot.Text, cbkec.Text)
      else if(sender=pdfbutton)then
      begin
        exportpdf2(dir, 'pdf',choose_format,ztable1,ZQuery1, ZQuery2, skabkot, skec, cbkabkot.Text, cbkec.Text);
      end
      else if(sender=printbutton)then
      begin
        exportpdf2(dir,'print',choose_format,ztable1,ZQuery1, ZQuery2, skabkot, skec, cbkabkot.Text, cbkec.Text);
      end
      else if(sender=pdfkabkot) then begin
                if (kodekabkot = '72') then begin //kota sukabumi
                  lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kotsukabumi'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA SUKABUMI';
                end
                else if (kodekabkot = '74') then begin //kota cirebon
                  lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kotcirebon'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA CIREBON';
                end
                else if (kodekabkot = '11') then begin //kabsumedang
                  lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kabsumedang'));
                  pemilihan:='PEMILIHAN BUPATI DAN WAKIL BUPATI SUMEDANG';
                end;
                lbg_jabar := Tfrxpictureview(frxReport1.FindObject('Picture2'));
                lbg_jabar.visible:=false;
                lbg_kabkot.Visible:=true;
                exportpdf2(dir, 'pdf',choose_format,ztable1,ZQuery1, ZQuery2, skabkot, skec, cbkabkot.Text, cbkec.Text);
              end
              else if(sender=printkabkot) then begin
                if (kodekabkot = '72') then begin //kota sukabumi
                  lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kotsukabumi'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA SUKABUMI';
                end
                else if (kodekabkot = '74') then begin //kota cirebon
                  lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kotcirebon'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA CIREBON';
                end
                else if (kodekabkot = '11') then begin //kabsumedang
                  lbg_kabkot := Tfrxpictureview(frxReport1.FindObject('kabsumedang'));
                  pemilihan:='PEMILIHAN BUPATI DAN WAKIL BUPATI SUMEDANG';
                end;
                lbg_jabar := Tfrxpictureview(frxReport1.FindObject('Picture2'));
                lbg_jabar.visible:=false;
                lbg_kabkot.Visible:=true;
                exportpdf2(dir,'print',choose_format,ztable1,ZQuery1, ZQuery2, skabkot, skec, cbkabkot.Text, cbkec.Text);
              end;
//      else if(sender=xlsbutton)then
//      begin
//        exportpdf2('xls',choose_format,ztable1,ZQuery1, ZQuery2, skabkot, skec, cbkabkot.Text, cbkec.Text);
//      end;
    end
    else
    begin
      showmessage('Lengkapi dulu form nya');
    end;
end;

procedure TTPS_Print_Form.cetakA1(Sender: TObject);
var ext : ExternalFile;
  idx:integer;
  namadesa,namakec,namakot, namatps, kodes:string;
  valid:boolean;
  dir:string;
  where:string;
  lbg_jabar, lbg_kabkot : Tfrxpictureview;
  list_keldes:tstringlist;
begin
  pemilihan:='PEMILIHAN GUBERNUR DAN WAKIL GUBERNUR JAWA BARAT';
    list_keldes := TStringList.Create;
    if(skeldes <> '') then
      list_keldes.Add(skeldes)
    else begin
      with zquery1 do
      begin
        active:=false;
        sql.Clear;
        if(skec <> '') then
          sql.Text:='SELECT KODE_DESA FROM desa WHERE KODE_KECAMATAN = '+quotedStr(skec)
        else
          sql.Text:='SELECT KODE_DESA FROM desa WHERE KODE_KABKOT = '+quotedStr(skabkot)+'';
        active:=true;
        first;
        while not eof do
        begin
          list_keldes.Add(fieldByName('KODE_DESA').AsString);
          next;
        end;
      end;
    end;
    idx:=0;
    for idx:=0 to list_keldes.Count-1 do begin
      kodes:=list_keldes[idx];
      with zquery1 do
      begin
        active:=false;
        sql.clear;

        sql.text:='update pemilih_'+kodes+' set KET='+quotedstr('')+
        ' where KET='+quotedstr('Ganda Satu Desa')+' or KET='+quotedstr('Ganda Antar Desa')+
        ' or KET='+quotedstr('Ganda Antar Kecamatan')+' or KET='+quotedstr('Ganda Antar Kab/Kota');
        execsql;
      end;
    end;
    list_keldes.Free;
    where:='';
    if((cbtps.ItemIndex<>0)and(cbtps2.ItemIndex<>0))then
    begin
      if(cbtps.Items[cbtps.ItemIndex]<=cbtps2.items[cbtps2.ItemIndex])then
        where:=' NAMA>='+quotedStr(cbtps.Items[cbtps.ItemIndex])+' and NAMA<='+quotedStr(cbtps2.Items[cbtps2.ItemIndex]);
      ///where:=' where NAMA<';
      //showmessage(where);
    end;
    //if(cbtps.ItemIndex=0)then
    //begin
      ZQuery2.active:=false;
      ZQuery2.sql.Clear;
      if(cbkabkot.ItemIndex=0)then
      begin
        ZQuery2.active:=false;
        ZQuery2.sql.Clear;
        if(where<>'')then
        where :=' where '+where;
        ZQuery2.sql.Text:='select * from daftar_tps'+where;
        valid:=true;
      end
      else if(cbkec.ItemIndex=0)then
      begin
        ZQuery2.active:=false;
        ZQuery2.sql.Clear;
        if(where<>'')then
        where:=' and '+where;
        ZQuery2.sql.Text:='select * from daftar_tps where KODE_DESA LIKE "'+skabkot+'%"';//+where;
        ZQuery2.active:=true;
        ZQuery2.first;
      end
      else if(cbkeldes.ItemIndex=0)then
      begin
        if(where<>'')then
        where:=' and '+where;
        ZQuery2.sql.Text:='select * from daftar_tps where KODE_DESA LIKE "'+skec+'%"'+where;
        ZQuery2.active:=true;
        ZQuery2.first;
      end
      else
      begin
        if(where<>'')then
        where:=' and '+where;
        ZQuery2.sql.Text:='select * from daftar_tps where KODE_DESA = '+quotedstr(skeldes)+' '+where;
        ZQuery2.active:=true;
        ZQuery2.first;
//        showmessage(skeldes);
      end;

      if(Zquery2.RecordCount>0)then
      begin
        if((Sender<>printbutton) and (Sender<>printkabkot))then
          SelectDirectory('Select a directory', 'C:\, D:\, E:\', dir);

        if((sender=xlsbutton)and(dir<>'')) then
        begin
          t_namadesa:=namadesa;
          t_namakec:=namakec;
          t_namakot:=namakot;
          t_namatps:=namatps;
          t_dir:=dir;
          JvThread1.ThreadDialog := JvThreadSimpleDialog1;
          JvThread1.Execute(Self);
        end
        else
        begin
          while (not ZQuery2.eof) do
          begin
            with ztable1 do
            begin
              active:=false;
              tablename:='desa';
              filter:='KODE_DESA='+QuotedStr(ZQuery2.FieldByName('KODE_DESA').AsString);
              filtered:=true;
              active:=true;
              first;
              namadesa:=FieldByName('TAMPIL').AsString;
              namakec:=FieldByName('KODE_KECAMATAN').AsString; // hanya sementara

              active:=false;
              tablename:='kecamatan';
              filter:='KODE_KECAMATAN='+QuotedStr(namakec);
              filtered:=true;
              active:=true;
              first;
              namakec:=FieldByName('TAMPIL').AsString;
              namakot:=FieldByName('KODE_KABKOT').AsString; // hanya sementara

              active:=false;
              tablename:='kabkot';
              filter:='KODE='+QuotedStr(namakot);
              filtered:=true;
              active:=true;
              first;
              namakot:=FieldByName('TAMPIL').AsString;
            end;
            //munculkan lambang jabar, hilangkan lambang lain
              //lbg jabar
              lbg_jabar := Tfrxpictureview(frxReport2.FindObject('Picture2'));
              lbg_jabar.visible:=true;
              //lbgkotacirebon
              lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotcirebon'));
              lbg_kabkot.Visible:=false;
              //lbgkabsumedang
              lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kabsumedang'));
              lbg_kabkot.Visible:=false;
              //lbgkotasukabumi
              lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotsukabumi'));
              lbg_kabkot.Visible:=false;
            if((sender=xlsbutton)and(dir<>'')) then
              printTPS(dir,'xls',choose_format, ZQuery2.FieldByName('KODE_DESA').AsString, ZQuery2.FieldByName('ID_TPS').AsString,
            namakot, namakec, namadesa, ZQuery2.FieldByName('NAMA').AsString)
            else if((sender=pdfbutton)and(dir<>''))then
              printTPS(dir,'pdf',choose_format, ZQuery2.FieldByName('KODE_DESA').AsString, ZQuery2.FieldByName('ID_TPS').AsString,
            namakot, namakec, namadesa, ZQuery2.FieldByName('NAMA').AsString)
            else if(sender=printbutton)then
              printTPS(dir,'print',choose_format, ZQuery2.FieldByName('KODE_DESA').AsString, ZQuery2.FieldByName('ID_TPS').AsString,
            namakot, namakec, namadesa, ZQuery2.FieldByName('NAMA').AsString)
            else if(sender=pdfkabkot) then begin
                if (kodekabkot = '72') then begin //kota sukabumi
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotsukabumi'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA SUKABUMI';
                end
                else if (kodekabkot = '74') then begin //kota cirebon
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotcirebon'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA CIREBON';
                end
                else if (kodekabkot = '11') then begin //kabsumedang
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kabsumedang'));
                  pemilihan:='PEMILIHAN BUPATI DAN WAKIL BUPATI SUMEDANG';
                end;
                lbg_jabar := Tfrxpictureview(frxReport2.FindObject('Picture2'));
                lbg_jabar.visible:=false;
                lbg_kabkot.Visible:=true;
                printTPS(dir,'pdf',choose_format, ZQuery2.FieldByName('KODE_DESA').AsString, ZQuery2.FieldByName('ID_TPS').AsString,
                namakot, namakec, namadesa, ZQuery2.FieldByName('NAMA').AsString);
              end
              else if(sender=printkabkot) then begin
                if (kodekabkot = '72') then begin //kota sukabumi
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotsukabumi'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA SUKABUMI';
                end
                else if (kodekabkot = '74') then begin //kota cirebon
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kotcirebon'));
                  pemilihan:='PEMILIHAN WALIKOTA DAN WAKIL WALIKOTA CIREBON';
                end
                else if (kodekabkot = '11') then begin //kabsumedang
                  lbg_kabkot := Tfrxpictureview(frxReport2.FindObject('kabsumedang'));
                  pemilihan:='PEMILIHAN BUPATI DAN WAKIL BUPATI SUMEDANG';
                end;
                lbg_jabar := Tfrxpictureview(frxReport2.FindObject('Picture2'));
                lbg_jabar.visible:=false;
                lbg_kabkot.Visible:=true;
                printTPS(dir,'print',choose_format, ZQuery2.FieldByName('KODE_DESA').AsString, ZQuery2.FieldByName('ID_TPS').AsString,
                namakot, namakec, namadesa, ZQuery2.FieldByName('NAMA').AsString);
              end;
            ZQuery2.next;
          end;
        end;
        if(sender=xlsbutton)then
          ShowFolder(dir+'\')
        else if(sender=pdfbutton)then
          ShowFolder(dir+'\');
      end
      else showmessage('Tidak terdapat TPS pada wilayah ini');
end;

procedure TTPS_Print_Form.printTPS(dir:string;typeext:string;model:integer;kodedesa:string;kodetps:string;textkabkot:string;textkec:string;textdes:string;texttps:string);
var ext : ExternalFile;
jumlah_row, sisa : integer;
sqls, kodes2, order : string;
desake : integer;
Page_row: TfrxReportPage;
lambang1: TfrxPictureView;
begin
  order:='ORDER BY STATUS1,RW,RT,NKK,NIK ASC';
  if(cbUrut.ItemIndex = 1) then //urut berdasarkan NIK saja
    order:='ORDER BY NIK ASC'
  else if(cbUrut.ItemIndex = 2) then //urut berdasarkan NKK saja
    order:='ORDER BY NKK ASC'
  else if(cbUrut.ItemIndex = 3) then //urut berdasarkan NAMA saja
    order:='ORDER BY NAMA ASC'
  else if(cbUrut.ItemIndex = 4) then //urut berdasarkan NAMA saja
    order:='ORDER BY DUSUN, RW, RT ASC'
  else if(cbUrut.ItemIndex = 5) then //urut berdasarkan NAMA saja
    order:='ORDER BY ID_USER ASC';
  if(((kodetps<>'')and(model<>0))or(model=0))then
  begin
    page_row := TfrxReportPage(frxReport2.findObject('Page4'));
    page_row.PrintOnPreviousPage := true;
    with ztable1 do
    begin
      active:=false;
      tablename:='info_tanggal';
      filter:='AKTIF='+QuotedStr('1');
      filtered:=true;
      active:=true;
      first;
    end;
    with Zquery1 do
    begin
      active:=false;
      sql.Clear;
      sql.Text:='select *,CONCAT(CONCAT(IF((STATUS1 = 1) OR (STATUS1 = 3), '+'''TAMBAHAN | '', ''''), IF(CACAT = 1,''Cacat Fisik'',IF(CACAT = 2,''Cacat Netra/Buta'',IF(CACAT=3,''Cacat Rungu Wicara'',IF(CACAT=4,''Cacat Mental/Jiwa'',IF(CACAT=5,'+'''Cacat Fisik Mental'',IF(CACAT=6,''Cacat Lainnya'',''''))))))), KET)  as SCACAT, DATE_FORMAT(FROM_DAYS(TO_DAYS('''+FormatDateTime('YYYY-MM-DD', ztable1.FieldByName('TANGGAL').AsDateTime) +''')-TO_DAYS(CONCAT(RIGHT(TANGGAL,4),''-'',MID(TANGGAL,4,2),''-'',LEFT(TANGGAL,2)))), ''%Y'')+0 AS age  from pemilih_'+kodedesa;

      //tablename:='pemilih_'+kodedesa;
      if(choose_format=0)then // A sementara tanpa tambahan
      begin
        sql.Text := sql.Text+' WHERE STATUS1=0 and (STATUS2=''0'') '+order;
        //sql.Text:=sql.Text+' WHERE STATUS1<>1 and (STATUS2=0 or STATUS2=4)';
      end
      else if((choose_format >=1) AND (choose_format <=4)) then
      begin
        //utk desa sendiri
        sqls:='SELECT * FROM (select *,CONCAT(CONCAT(IF((STATUS1 = 1) OR (STATUS1 = 3), '+'''TAMBAHAN | '', ''''), IF(CACAT = 1,''Cacat Fisik'',IF(CACAT = 2,''Cacat Netra/Buta'',IF(CACAT=3,''Cacat Rungu Wicara'',IF(CACAT=4,''Cacat Mental/Jiwa'',IF(CACAT=5,'+'''Cacat Fisik Mental'',IF(CACAT=6,''Cacat Lainnya'',''''))))))), KET)  as SCACAT, DATE_FORMAT(FROM_DAYS(TO_DAYS('''+FormatDateTime('YYYY-MM-DD', ztable1.FieldByName('TANGGAL').AsDateTime) +''')-TO_DAYS(CONCAT(RIGHT(TANGGAL,4),''-'',MID(TANGGAL,4,2),''-'',LEFT(TANGGAL,2)))), ''%Y'')+0 AS age  from pemilih_'+kodedesa;
        if(choose_format=1)then // A1 tanpa tambahan
        begin
          sqls:=sqls+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1<>''1'' and STATUS1<>''3'' and (STATUS2=''0'') '+order+')';
        end
        else if(choose_format=2)then // A2 tambahan
        begin
          sqls:=sqls+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=''1'' and (STATUS2=''0'') '+order+')';
        end
        else if(choose_format=3)then // A3 nyatu
        begin
          sqls:=sqls+' WHERE TPS='+QuotedStr(kodetps)+' and  (STATUS2=''0'') '+order+')';
        end
        else if(choose_format=4)then // A4 sama kaya A3
        begin
          sqls:=sqls+' WHERE TPS='+QuotedStr(kodetps)+' and (STATUS2=''0'') '+order+')';
        end;
        sqls:= sqls+ ' as DESA0 UNION ALL ';
        desake := 0;
        tblAntara.Active:=false;
        tblAntara.TableName:='desa';
        tblAntara.Filter:='KODE_KECAMATAN = '+quotedStr(copy(kodedesa,1,5));
        tblAntara.Filtered:=true;
        tblAntara.Active:=true;
        tblAntara.First;
        while (not tblAntara.Eof) do begin
          kodes2 := tblAntara.fieldByName('KODE_DESA').AsString;
          if (kodes2 <> kodedesa) then begin
            desake:=desake+1;
            sqls:=sqls+'SELECT * FROM (select *,CONCAT(CONCAT(IF((STATUS1 = 1) OR (STATUS1 = 3), '+'''TAMBAHAN | '', ''''), IF(CACAT = 1,''Cacat Fisik'',IF(CACAT = 2,''Cacat Netra/Buta'',IF(CACAT=3,''Cacat Rungu Wicara'',IF(CACAT=4,''Cacat Mental/Jiwa'',IF(CACAT=5,'+'''Cacat Fisik Mental'',IF(CACAT=6,''Cacat Lainnya'',''''))))))), KET)  as SCACAT, DATE_FORMAT(FROM_DAYS(TO_DAYS('''+FormatDateTime('YYYY-MM-DD', ztable1.FieldByName('TANGGAL').AsDateTime) +''')-TO_DAYS(CONCAT(RIGHT(TANGGAL,4),''-'',MID(TANGGAL,4,2),''-'',LEFT(TANGGAL,2)))), ''%Y'')+0 AS age  from pemilih_'+kodes2;
            if(choose_format=1)then // A1 tanpa tambahan
            begin
              sqls:=sqls+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1<>1 and (STATUS2=0) '+order+')';
            end
            else if(choose_format=2)then // A2 tambahan
            begin
              sqls:=sqls+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=1 and (STATUS2=0) '+order+')';
            end
            else if(choose_format=3)then // A3 nyatu
            begin
              sqls:=sqls+' WHERE TPS='+QuotedStr(kodetps)+' and (STATUS2=0) '+order+')';
            end
            else if(choose_format=4)then // A4 sama kaya A3
            begin
              sqls:=sqls+' WHERE TPS='+QuotedStr(kodetps)+' and (STATUS2=0) '+order+')';
            end;
            if(desake < tblAntara.RecordCount - 1) then
              sqls:= sqls+ ' as DESA'+inttostr(desake)+' UNION ALL '
            else
              sqls:= sqls+ ' as DESA'+inttostr(desake);
          end;
          tblAntara.Next;
        end;
        memo1.Text:=sqls;
        sql.Text:=sqls;
      end
      else if(choose_format=5)then // A5 rekap kecamatan
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=1 and (STATUS2=0)';
      end
      else if(choose_format=6)then // A6 rekap kota
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=1 and (STATUS2=0)';
      end
      else if(choose_format=7)then // A7 rekap provinsi
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=1 and (STATUS2=0)';
      end;

      //filtered:=true;
      active:=true;
      first;

      jumlah_row := RecordCount;
      jumlah_row := jumlah_row - 20;

      //page_row := TfrxReportPage(frxReport2.Pages[1]);
      //lambang1 := Tfrxpictureview(frxReport2.FindObject('Picture2'));
      //lambang1.Visible:=false;
      
      if(jumlah_row > 0) then begin
//        showmessage(inttostr(jumlah_row));
          //showmessage(frxReport2.Pages[3].Name);
        sisa := jumlah_row mod 25;
        //showmessage(inttostr(sisa));
        //showmessage(inttostr(ceil(frxReport2.findObject('Page2').Height)));
        if((sisa = 0) or (sisa > 15)) then begin
          page_row := TfrxReportPage(frxReport2.findObject('Page4'));
          page_row.PrintOnPreviousPage := false;
        end;
      end;

      ext:=ExternalFile.Create;
      
    end;

    if(typeext='xls')then
    begin
      t_dir:=dir;
      t_model:=model;
      t_textkabkot:=textkabkot;
      t_textkec:=textkec;
      t_textdes:=textdes;
      t_texttps:=texttps;

      //JvThread1.ThreadDialog := JvThreadSimpleDialog1;
      //JvThread1.Execute(Self);

      ext.exportExcel(dir,model,ZQuery1,ztable1, textkabkot, textkec, textdes, texttps, skeldes)
    end
    else begin // export pdf
      if(model=0)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('DATA PEMILIH');
      end
      else if(model=1)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A1 - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('DAFTAR PEMILIH SEMENTARA');

      end
      else if(model=2)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A2 - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('DAFTAR PEMILIH TAMBAHAN');

      end
      else if(model=3)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A3 - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('DAFTAR PEMILIH TETAP');

      end
      else if(model=4)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A4 - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('SALINAN DAFTAR PEMILIH TETAP UNTUK TPS');
      end;
      frxReport2.Variables['JUDUL2'] :=QuotedStr(pemilihan);
      frxReport2.Variables['JUDUL3'] :=QuotedStr('TAHUN 2013');
      if(copy(texttps,1,1) = 'K') then
        frxReport2.Variables['TPS'] := QuotedStr('TPS KHUSUS '+copy(texttps,2,2))
      else
        frxReport2.Variables['TPS'] :=QuotedStr(texttps);
      frxReport2.Variables['KECAMATAN'] :=QuotedStr(textkec);
      frxReport2.Variables['KABKOTA'] :=QuotedStr(textkabkot);
      //frxReport2.Variables['HALAMAN'] :=QuotedStr('.........');
      frxReport2.Variables['KETUA'] :=QuotedStr('.........');
      frxReport2.Variables['ANGGOTA1'] :=QuotedStr('.........');
      frxReport2.Variables['ANGGOTA2'] :=QuotedStr('.........');

      with ztable2 do
      begin
        active:=false;
        tablename:='petugas';
        filter:='KODE_DESA='+QuotedStr(kodedesa)+' and POSISI=4';
        filtered:=true;
        active:=true;
        first;
        while (not eof) do
        begin
          if(fieldbyname('JABATAN').asstring = '1') then
          begin
            frxReport2.Variables['KETUA'] :=QuotedStr(fieldbyname('NAMA').asstring);
          end;
          if(fieldbyname('JABATAN').asstring='2')then
          begin
            if(frxReport2.Variables['ANGGOTA1'] =QuotedStr('.........'))then
              frxReport2.Variables['ANGGOTA1'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport2.Variables['ANGGOTA2'] =QuotedStr('.........'))then
              frxReport2.Variables['ANGGOTA2'] :=QuotedStr(fieldbyname('NAMA').asstring)
          end;
          next;
        end;
      end;


      // Save the Workbook
      if not DirectoryExists(dir+'\Excel\') then CreateDir(dir+'\Excel\');
      if not DirectoryExists(dir+'\Excel\'+textkabkot) then CreateDir(dir+'\Excel\'+textkabkot);
      if not DirectoryExists(dir+'\Excel\'+textkabkot+'\'+textkec) then CreateDir(dir+'\Excel\'+textkabkot+'\'+textkec);
      //if not DirectoryExists(dir+'\Excel\'+textkabkot+'\'+textkec+'\'+textdes) then CreateDir(dir+'\Excel\'+textkabkot+'\'+textkec+'\'+textdes);

      if not DirectoryExists(dir+'\Pdf\') then CreateDir(dir+'\Pdf\');
      if not DirectoryExists(dir+'\Pdf\'+textkabkot) then CreateDir(dir+'\Pdf\'+textkabkot);
      if not DirectoryExists(dir+'\Pdf\'+textkabkot+'\'+textkec) then CreateDir(dir+'\Pdf\'+textkabkot+'\'+textkec);
      //if not DirectoryExists(dir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes) then CreateDir(dir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes);


      frxDBDataset3.DataSet:=Zquery1;
      frxReport2.Variables['DESA']:=QuotedStr(textdes);
      if(typeext='pdf')then
      begin
        frxReport2.PrintOptions.ShowDialog:=false;
        frxReport2.PrepareReport;
        frxPDFExport1.ShowDialog:=false;
        if(model<>0)then
          frxPDFExport1.FileName:=dir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes+'-TPS_'+texttps+'.pdf'
        else
          frxPDFExport1.FileName:=dir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes+'-formA.pdf';
        //frxPDFExport1.OpenAfterExport:=true;

        //ShowFolder(GetCurrentDir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes+'\');

        frxReport2.Export(frxPDFExport1);
      end
//      else if(typeext='xls')then
//      begin
//        frxReport2.PrintOptions.ShowDialog:=false;
//        frxReport2.PrepareReport;
//        frxXLSExport1.ShowDialog:=false;
//        frxXLSExport1.OpenExcelAfterExport:=true;
//        frxXLSExport1.FileName:=GetCurrentDir+'\Excel\'+textkabkot+'\'+textkec+'\'+textdes+'\TPS_'+texttps+'.xls';
//        frxReport2.Export(frxXLSExport1);
//      end
      else begin
        frxReport2.ShowReport(true);

        //frxReport2.PrintOptions.ShowDialog:=false;

        //frxReport2.ReportOptions.Name:='something.pdf';

        //if frxReport2.PrepareReport(true) then
        //  frxReport2.Print;
      end;

    end;

  end;
end;

procedure TTPS_Print_Form.printTPSperKec(dir:string;typeext:string;model:integer;kodekec:string;kodetps:string;textkabkot:string;textkec:string;textdes:string;texttps:string);
var ext : ExternalFile;
kodedesa :string;
jumlah_row, sisa : integer;
Page_row: TfrxReportPage;
begin
  //jika model A
  if model=0 then
  begin
    with ztable3 do
    begin
      active:=false;
      tablename:='desa';
      filter:='KODE_KECAMATAN = '+quotedStr(skec);
      filtered:=true;
      active:=true;
      first;
      while not eof do begin
        kodedesa := FieldByName('KODE_DESA').AsString;
        showmessage(kodedesa);
        next;
      end;
    end;
  end;
  {if(((kodetps<>'')and(model<>0))or(model=0))then
  //iterasi per kecamatan
  //.....
  kodedesa = '';
  begin
    with ztable1 do
    begin
      active:=false;
      tablename:='info_tanggal';
      filter:='AKTIF='+QuotedStr('1');
      filtered:=true;
      active:=true;
      first;
    end;
    with Zquery1 do
    begin
      active:=false;
      sql.Clear;
      sql.Text:='select *, DATE_FORMAT(FROM_DAYS(TO_DAYS('''+FormatDateTime('YYYY-MM-DD', ztable1.FieldByName('TANGGAL').AsDateTime) +''')-TO_DAYS(CONCAT(RIGHT(TANGGAL,4),''-'',MID(TANGGAL,4,2),''-'',LEFT(TANGGAL,2)))), ''%Y'')+0 AS age  from pemilih_'+kodedesa;

      //tablename:='pemilih_'+kodedesa;
      if(choose_format=0)then // A sementara tanpa tambahan
      begin
        sql.Text := sql.Text+' WHERE STATUS1<>1 and (STATUS2=0) ORDER BY RW,RT,NKK ASC';
        //sql.Text:=sql.Text+' WHERE STATUS1<>1 and (STATUS2=0 or STATUS2=4)';
      end
      else if(choose_format=1)then // A1 tanpa tambahan
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1<>1 and (STATUS2=0) ORDER BY RW,RT,NKK ASC';
      end
      else if(choose_format=2)then // A2 tambahan
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=1 and (STATUS2=0) ORDER BY RW,RT,NKK ASC';
      end
      else if(choose_format=3)then // A3 nyatu
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and  (STATUS2=0) ORDER BY RW,RT,NKK ASC';
      end
      else if(choose_format=4)then // A4 sama kaya A3
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and (STATUS2=0) ORDER BY RW,RT,NKK ASC';
      end
      else if(choose_format=5)then // A5 rekap kecamatan
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=1 and (STATUS2=0)';
      end
      else if(choose_format=6)then // A6 rekap kota
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=1 and (STATUS2=0)';
      end
      else if(choose_format=7)then // A7 rekap provinsi
      begin
        sql.Text:=sql.Text+' WHERE TPS='+QuotedStr(kodetps)+' and STATUS1=1 and (STATUS2=0)';
      end;

      //filtered:=true;
      active:=true;
      first;

      jumlah_row := RecordCount;
      jumlah_row := jumlah_row - 27;
      if(jumlah_row > 0) then begin
//        showmessage(inttostr(jumlah_row));
          //showmessage(frxReport2.Pages[3].Name);
        sisa := jumlah_row mod 31;
        if((sisa <> 0) and (sisa <= 20)) then begin
          page_row := TfrxReportPage(frxReport2.Pages[3]);
          page_row.PrintOnPreviousPage := true;
        end;
      end;

      ext:=ExternalFile.Create;

    end;

    if(typeext='xls')then
    begin
      t_dir:=dir;
      t_model:=model;
      t_textkabkot:=textkabkot;
      t_textkec:=textkec;
      t_textdes:=textdes;
      t_texttps:=texttps;

      //JvThread1.ThreadDialog := JvThreadSimpleDialog1;
      //JvThread1.Execute(Self);

      ext.exportExcel(dir,model,ZQuery1,ztable1, textkabkot, textkec, textdes, texttps, skeldes)
    end
    else begin // export pdf
      if(model=0)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('DATA PEMILIH');
      end
      else if(model=1)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A1 - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('DAFTAR PEMILIH SEMENTARA');

      end
      else if(model=2)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A2 - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('DAFTAR PEMILIH TAMBAHAN');

      end
      else if(model=3)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A3 - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('DAFTAR PEMILIH TETAP');

      end
      else if(model=4)then
      begin
        // Model A
        frxReport2.Variables['MODEL'] :=QuotedStr('MODEL A4 - KWK.KPU');
        frxReport2.Variables['JUDUL1'] :=QuotedStr('SALINAN DAFTAR PEMILIH TETAP UNTUK TPS');
      end;
      frxReport2.Variables['JUDUL2'] :=QuotedStr('PEMILIHAN GUBERNUR DAN WAKIL GUBERNUR JAWA BARAT');
      frxReport2.Variables['JUDUL3'] :=QuotedStr('TAHUN 2013');

      frxReport2.Variables['TPS'] :=QuotedStr(texttps);
      frxReport2.Variables['KECAMATAN'] :=QuotedStr(textkec);
      frxReport2.Variables['KABKOTA'] :=QuotedStr(textkabkot);
      //frxReport2.Variables['HALAMAN'] :=QuotedStr('.........');
      frxReport2.Variables['KETUA'] :=QuotedStr('.........');
      frxReport2.Variables['ANGGOTA1'] :=QuotedStr('.........');
      frxReport2.Variables['ANGGOTA2'] :=QuotedStr('.........');

      with ztable2 do
      begin
        active:=false;
        tablename:='petugas';
        filter:='KODE_DESA='+QuotedStr(kodedesa)+' and POSISI=4';
        filtered:=true;
        active:=true;
        first;
        while (not eof) do
        begin
          if(fieldbyname('JABATAN').asstring = '1') then
          begin
            frxReport2.Variables['KETUA'] :=QuotedStr(fieldbyname('NAMA').asstring);
          end;
          if(fieldbyname('JABATAN').asstring='2')then
          begin
            if(frxReport2.Variables['ANGGOTA1'] =QuotedStr('.........'))then
              frxReport2.Variables['ANGGOTA1'] :=QuotedStr(fieldbyname('NAMA').asstring)
            else if(frxReport2.Variables['ANGGOTA2'] =QuotedStr('.........'))then
              frxReport2.Variables['ANGGOTA2'] :=QuotedStr(fieldbyname('NAMA').asstring)
          end;
          next;
        end;
      end;


      // Save the Workbook
      if not DirectoryExists(dir+'\Excel\') then CreateDir(dir+'\Excel\');
      if not DirectoryExists(dir+'\Excel\'+textkabkot) then CreateDir(dir+'\Excel\'+textkabkot);
      if not DirectoryExists(dir+'\Excel\'+textkabkot+'\'+textkec) then CreateDir(dir+'\Excel\'+textkabkot+'\'+textkec);
      if not DirectoryExists(dir+'\Excel\'+textkabkot+'\'+textkec+'\'+textdes) then CreateDir(dir+'\Excel\'+textkabkot+'\'+textkec+'\'+textdes);

      if not DirectoryExists(dir+'\Pdf\') then CreateDir(dir+'\Pdf\');
      if not DirectoryExists(dir+'\Pdf\'+textkabkot) then CreateDir(dir+'\Pdf\'+textkabkot);
      if not DirectoryExists(dir+'\Pdf\'+textkabkot+'\'+textkec) then CreateDir(dir+'\Pdf\'+textkabkot+'\'+textkec);
      if not DirectoryExists(dir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes) then CreateDir(dir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes);


      frxDBDataset3.DataSet:=Zquery1;
      frxReport2.Variables['Desa']:=QuotedStr(textdes);
      if(typeext='pdf')then
      begin
        frxReport2.PrintOptions.ShowDialog:=false;
        frxReport2.PrepareReport;
        frxPDFExport1.ShowDialog:=false;
        if(model<>0)then
          frxPDFExport1.FileName:=dir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes+'\TPS_'+texttps+'.pdf'
        else
          frxPDFExport1.FileName:=dir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes+'-forma.pdf';
        //frxPDFExport1.OpenAfterExport:=true;

        //ShowFolder(GetCurrentDir+'\Pdf\'+textkabkot+'\'+textkec+'\'+textdes+'\');

        frxReport2.Export(frxPDFExport1);
      end
//      else if(typeext='xls')then
//      begin
//        frxReport2.PrintOptions.ShowDialog:=false;
//        frxReport2.PrepareReport;
//        frxXLSExport1.ShowDialog:=false;
//        frxXLSExport1.OpenExcelAfterExport:=true;
//        frxXLSExport1.FileName:=GetCurrentDir+'\Excel\'+textkabkot+'\'+textkec+'\'+textdes+'\TPS_'+texttps+'.xls';
//        frxReport2.Export(frxXLSExport1);
//      end
      else begin
        frxReport2.ShowReport(true);

        //frxReport2.PrintOptions.ShowDialog:=false;

        //frxReport2.ReportOptions.Name:='something.pdf';

        //if frxReport2.PrepareReport(true) then
        //  frxReport2.Print;
      end;

    end;

  end; }
end;

procedure TTPS_Print_Form.RadioButton1Click(Sender: TObject);
begin
    cbtps.Enabled:=true;
    cbkeldes.Enabled:=true;
    if(kodekec='')then cbkec.Enabled:=true;
    if(kodekabkot='00')then cbkabkot.Enabled:=true;
    choose_format:=0;
    cbkeldes.ItemIndex:=0;
    cbtps.ItemIndex:=0;
    cbtps2.ItemIndex:=0;
    printbutton.Enabled:=false;
    pdfbutton.Enabled:=false;
    xlsbutton.Enabled:=false;

    cbtps.Enabled:=false;
    cbtps2.Enabled:=false;


end;

procedure TTPS_Print_Form.RadioButton2Click(Sender: TObject);
begin
    cbtps.Enabled:=true;
    cbkeldes.Enabled:=true;
    if(kodekec='')then cbkec.Enabled:=true;
    if(kodekabkot='00')then cbkabkot.Enabled:=true;
    choose_format:=1;
    cbtps.ItemIndex:=0;
    cbtps2.ItemIndex:=0;
    printbutton.Enabled:=false;
    //pdfbutton.Enabled:=false;
    //xlsbutton.Enabled:=false;

    cbtps.Enabled:=true;
    cbtps2.Enabled:=true;


end;

procedure TTPS_Print_Form.RadioButton3Click(Sender: TObject);
begin
    cbtps.Enabled:=true;
    cbkeldes.Enabled:=true;
    if(kodekec='')then cbkec.Enabled:=true;
    if(kodekabkot='00')then cbkabkot.Enabled:=true;
    choose_format:=2;
    cbtps.ItemIndex:=0;
    cbtps2.ItemIndex:=0;
    printbutton.Enabled:=false;
    pdfbutton.Enabled:=false;
    xlsbutton.Enabled:=false;
    cbtps.Enabled:=true;
    cbtps2.Enabled:=true;

end;

procedure TTPS_Print_Form.RadioButton4Click(Sender: TObject);
begin
    cbtps.Enabled:=true;
    cbkeldes.Enabled:=true;
    if(kodekec='')then cbkec.Enabled:=true;
    if(kodekabkot='00')then cbkabkot.Enabled:=true;
    cbtps.ItemIndex:=0;
    cbtps2.ItemIndex:=0;
    printbutton.Enabled:=false;
    pdfbutton.Enabled:=false;
    xlsbutton.Enabled:=false;
    cbtps.Enabled:=true;
    cbtps2.Enabled:=true;

    choose_format:=3;
end;

procedure TTPS_Print_Form.RadioButton5Click(Sender: TObject);
begin
    cbtps.Enabled:=true;
    cbkeldes.Enabled:=true;
    if(kodekec='')then cbkec.Enabled:=true;
    if(kodekabkot='00')then cbkabkot.Enabled:=true;
    choose_format:=4;
    cbtps.ItemIndex:=0;
    cbtps2.ItemIndex:=0;
    printbutton.Enabled:=false;
    pdfbutton.Enabled:=false;
    xlsbutton.Enabled:=false;
    cbtps.Enabled:=true;
    cbtps2.Enabled:=true;
end;

procedure TTPS_Print_Form.RadioButton6Click(Sender: TObject);
begin
    cbtps.Enabled:=false;
    cbkeldes.Enabled:=false;
    if(kodekec='')then
    begin
      cbkec.Enabled:=true;
      cbkec.itemindex := 0;
    end;
    if(kodekabkot='00')then cbkabkot.Enabled:=true;
    choose_format:=5;
    printbutton.Enabled:=false;
  pdfbutton.Enabled:=false;
  xlsbutton.Enabled:=false;
    cbtps.Enabled:=false;
    cbtps2.Enabled:=false;
    if(kodekec <> '') then begin
      printbutton.Enabled:=true;
      pdfbutton.Enabled:=true;
      xlsbutton.Enabled:=true;
    end;


end;

procedure TTPS_Print_Form.RadioButton7Click(Sender: TObject);
begin
    cbtps.Enabled:=false;
    cbkeldes.Enabled:=false;
    cbkec.Enabled:=false;
    if(kodekabkot='00')then
    begin
      cbkabkot.Enabled:=true;
      cbkabkot.ItemIndex:=0;
      printbutton.Enabled:=false;
      pdfbutton.Enabled:=false;
      xlsbutton.Enabled:=false;
    end
    else begin
      printbutton.Enabled:=true;
      pdfbutton.Enabled:=true;
      xlsbutton.Enabled:=true;
    end;
    choose_format:=6;
    cbtps.Enabled:=false;
    cbtps2.Enabled:=false;


end;

procedure TTPS_Print_Form.RadioButton8Click(Sender: TObject);
begin
    cbtps.Enabled:=false;
    cbkeldes.Enabled:=false;
    cbkec.Enabled:=false;
    cbkabkot.Enabled:=false;
    choose_format:=7;
    printbutton.Enabled:=true;
    pdfbutton.Enabled:=true;
    xlsbutton.Enabled:=true;
        cbtps.Enabled:=false;
    cbtps2.Enabled:=false;


end;

procedure TTPS_Print_Form.Button4Click(Sender: TObject);
begin
close;
end;

procedure TTPS_Print_Form.cbformatChange(Sender: TObject);
begin
  if(choose_format<5)then
  begin
    cbtps.Enabled:=true;
    cbkeldes.Enabled:=true;
    cbkec.Enabled:=true;
    cbkabkot.Enabled:=true;

  end
  else if(choose_format=5)then
  begin
    cbtps.Enabled:=false;
    cbkeldes.Enabled:=false;
    cbkec.Enabled:=true;
    cbkabkot.Enabled:=true;

  end
  else if(choose_format=6)then
  begin
    cbtps.Enabled:=false;
    cbkeldes.Enabled:=false;
    cbkec.Enabled:=false;
    cbkabkot.Enabled:=true;

  end
  else if(choose_format=7)then
  begin
    cbtps.Enabled:=false;
    cbkeldes.Enabled:=false;
    cbkec.Enabled:=false;
    cbkabkot.Enabled:=false;
  end;
end;

procedure TTPS_Print_Form.cbkabkotChange(Sender: TObject);
var pilihan:string;
begin
  if (cbKabkot.ItemIndex > 0) then
  begin
    pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
    with ZTable1 do
      begin
        Active := False;
        TableName := 'kabkot';
        Filter := 'TAMPIL = '+QuotedStr(pilihan);
        Filtered := True;
        Active := True;
        First;
        sKabkot := FieldByName('KODE').AsString;
        //cb2
        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(sKabkot);
        Filtered := True;
        Active := True;
        First;
      end;
    cbKec.Clear;
    cbKec.Items.Add('SEMUA KECAMATAN');
    cbKec.ItemIndex:=0;
    cbkeldes.Clear;
    cbkeldes.Items.Add('SEMUA KELURAHAN/DESA');
    cbkeldes.ItemIndex:=0;
    cbtps.Clear;
    cbTPS.Items.Add('Pilih TPS');
    cbtps.ItemIndex:=0;

    while (not ZTable1.Eof) do
      begin
        cbKec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKabkot:='';
    cbKec.Clear;
    cbkeldes.Clear;
  end;
  sKec:='';
  skeldes:='';
  printbutton.Enabled:=false;
  pdfbutton.Enabled:=false;
  xlsbutton.Enabled:=false;
  if ((cbKabkot.ItemIndex > 0)and(choose_format=6)) then
  begin
  printbutton.Enabled:=true;
  pdfbutton.Enabled:=true;
  xlsbutton.Enabled:=true;

  end;

end;

procedure TTPS_Print_Form.cbkecChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbKec.ItemIndex > 0) then
  begin
    pilihan := cbKec.Items[cbKec.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'kecamatan';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(pilihan)+' AND KODE_KABKOT='+QuotedStr(skabkot);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sKec := ZTable1.FieldByName('KODE_KECAMATAN').AsString;
    //cb3
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'KODE_KECAMATAN = '+QuotedStr(sKec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    cbkeldes.Clear;
    cbkeldes.Items.Add('PILIH KELURAHAN/DESA');
    cbkeldes.ItemIndex:=0;
    cbtps.Clear;
    cbTPS.Items.Add('Pilih TPS');
    cbtps.ItemIndex:=0;

    while (not ZTable1.Eof) do
      begin
        cbkeldes.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
    if(choose_format >= 0) and (choose_format <= 4) then begin //jika yg aktif form A
      //aktifkan tombol-tombol
      xlsbutton.enabled := true;
      pdfbutton.enabled := true;
      printbutton.Enabled := true;
    end;
  end
    else
  begin
    sKec:='';
    cbkeldes.Clear;
    xlsbutton.enabled := false;
    pdfbutton.enabled := false;
    printbutton.Enabled := false;
  end;
  skeldes:='';
    {printbutton.Enabled:=false;
    pdfbutton.Enabled:=false;
    xlsbutton.Enabled:=false;}
  if((choose_format=5)and(cbKec.ItemIndex > 0))then
  begin
    printbutton.Enabled:=true;
    pdfbutton.Enabled:=true;
    xlsbutton.Enabled:=true;

  end;

end;

procedure TTPS_Print_Form.cbkeldesChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbkeldes.ItemIndex > 0) then
  begin
    pilihan := cbkeldes.Items[cbkeldes.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(pilihan)+' AND KODE_KECAMATAN='+QuotedStr(skec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    skeldes := ZTable1.FieldByName('KODE_DESA').AsString;
    //showmessage(skeldes);
    //cb3
    ZTable1.Active := False;
    ZTable1.TableName := 'daftar_tps';
    ZTable1.Filter := 'KODE_DESA = '+QuotedStr(skeldes);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    cbtps.Clear;
    cbTPS.Items.Add('Pilih TPS');
    cbtps.ItemIndex:=0;
    cbtps2.Clear;
    cbTPS2.Items.Add('Pilih TPS');
    cbtps2.ItemIndex:=0;
    while (not ZTable1.Eof) do
      begin
        cbtps.Items.Add(ZTable1.FieldByName('NAMA').AsString);
        cbtps2.Items.Add(ZTable1.FieldByName('NAMA').AsString);
        ZTable1.Next;
      end;


    //skelDes := ZTable1.FieldByName('KODE_DESA').AsString;
    //showmessage('nih '+skeldes);
    printbutton.Enabled:=true;
    pdfbutton.Enabled:=true;
    xlsbutton.Enabled:=true;

  end
    else
  begin
    skelDes := '';
    cbtps.Clear;
    cbTPS.Items.Add('pilih TPS');
    cbtps.ItemIndex:=0;
    printbutton.Enabled:=false;
    pdfbutton.Enabled:=false;
    xlsbutton.Enabled:=false;


  end;
  stps:='';
end;

procedure TTPS_Print_Form.cbtpsChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbtps.ItemIndex > 0) then
  begin
    pilihan := cbtps.Items[cbtps.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'daftar_tps';
    ZTable1.Filter := 'KODE_DESA = '+QuotedStr(skeldes)+' and NAMA='+QuotedStr(pilihan);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    stps:=ZTable1.FieldByName('ID_TPS').AsString;
  end
    else
  begin
    stps:='';
  end;

end;

procedure TTPS_Print_Form.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TTPS_Print_Form.FormCreate(Sender: TObject);
var setting:TSetting_Form;
query:string;
id_proses:integer;
f:tfield;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ztable2.Connection:=ZConnection1;
  ztable3.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  ZQuery2.Connection:=ZConnection1;
  ZQuery3.Connection:=ZConnection1;
  ZQuery4.Connection:=ZConnection1;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;
  choose_format:=0;

  cbtps.Enabled:=false;
  cbtps2.Enabled:=false;
  {
  zquery1.active:=false;
  query:= 'SELECT a.PROSES, a.TANGGAL_SELESAI, b.KETERANGAN FROM proses_berjalan a INNER JOIN info_tanggal b ON a.ID_INFO = b.ID WHERE a.TANGGAL_MULAI <= NOW() AND a.TANGGAL_SELESAI >= NOW()';
  ZQuery1.SQL.Text := query;
  zquery1.active:=true;
  zquery1.first;
  if(zquery1.RecordCount>0)then begin
    while (not ZQuery1.Eof) do begin
      //id_proses := 5;//ZQuery1.Fields[0].AsInteger;
      //proses_berjalan := id_proses;
      case id_proses of
        1 :
          begin
            RadioButton1.Checked:=true;
            RadioButton1.Enabled:=true;
            RadioButton2.Enabled:=false;
            RadioButton3.Enabled:=false;
            RadioButton4.Enabled:=false;
            RadioButton5.Enabled:=false;
            RadioButton6.Enabled:=false;
            RadioButton7.Enabled:=false;
            RadioButton8.Enabled:=false;
            //File1.Enabled:=true;
          end;
        2 :
          begin
            RadioButton2.Checked:=true;
            RadioButton2.Enabled:=true;
            RadioButton1.Enabled:=false;
            RadioButton3.Enabled:=false;
            RadioButton4.Enabled:=false;
            RadioButton5.Enabled:=false;
            RadioButton6.Enabled:=false;
            RadioButton7.Enabled:=false;
            RadioButton8.Enabled:=false;
            //Window1.Enabled:=true;
          end;
        3 :
          begin
            RadioButton3.Checked:=true;
            RadioButton3.Enabled:=true;
            RadioButton2.Enabled:=false;
            RadioButton1.Enabled:=false;
            RadioButton4.Enabled:=false;
            RadioButton5.Enabled:=false;
            RadioButton6.Enabled:=false;
            RadioButton7.Enabled:=false;
            RadioButton8.Enabled:=false;
            //Help1.Enabled:=true;
            //Window1.Enabled:=true;
          end;
        4 :
          begin
            RadioButton4.Checked:=true;
            RadioButton1.Enabled:=false;
            RadioButton2.Enabled:=false;
            RadioButton3.Enabled:=false;
            RadioButton4.Enabled:=true;
            RadioButton5.Enabled:=true;
            RadioButton6.Enabled:=true;
            RadioButton7.Enabled:=true;
            RadioButton8.Enabled:=true;
            //PenyusunandanPerbaikanDPT1.Enabled:=true;
          end;
        5 : // ngasal
          begin

            {RadioButton4.Checked:=true;
            RadioButton1.Enabled:=true;
            RadioButton2.Enabled:=true;
            RadioButton3.Enabled:=true;
            RadioButton4.Enabled:=true;
            RadioButton5.Enabled:=true;
            RadioButton6.Enabled:=true;
            RadioButton7.Enabled:=true;
            RadioButton8.Enabled:=true;
          end;
      end;
//      showMessage(nama_proses);
      zquery1.Next;
    end;
  end;                                 }

  if(kodeKec<>'')then
  begin
    if ((kodekabkot = '11') or (kodekabkot = '74') or (kodekabkot = '72')) then
    begin
      printkabkot.Visible:=true;
      pdfkabkot.Visible:=true;
    end;
    //RadioButton6.Enabled:=false;
    RadioButton7.Enabled:=false;
    RadioButton8.Enabled:=false;
  end
  else if (kodekabkot<>'00')then
  begin
    //RadioButton7.Enabled:=false;
    if ((kodekabkot = '11') or (kodekabkot = '74') or (kodekabkot = '72')) then
    begin
      printkabkot.Visible:=true;
      pdfkabkot.Visible:=true;
    end;
    RadioButton8.Enabled:=false;
  end;

  BorderIcons := BorderIcons - [biMaximize];
  cbkabkot.items.Add('Pilih Kabupaten/Kota');
  cbkec.items.Add('Pilih Kecamatan');
  cbkeldes.items.Add('Pilih Kelurahan/Desa');
  cbkabkot.ItemIndex:=0;
  cbkec.ItemIndex:=0;
  cbkeldes.ItemIndex:=0;
//  cbTPS.Items.Add('Semua TPS');
  //cbtps.ItemIndex:=0;


  skabkot:='';
  sKec:='';
  skeldes:='';
  stps :='';

  if(kodekec<>'')then // petugas kecamatan
  begin
    cbkabkot.enabled:=false;
    cbkec.enabled:=false;
    with ZTable1 do begin
      Active:=False;
      TableName:='kabkot';
      filter:='KODE='+quotedstr(kodekabkot);
      filtered:=true;
      Active:=True;
      cbkabkot.items.add(fieldbyname('TAMPIL').asstring);
      cbkabkot.itemindex:=1;
      sKabkot:=kodekabkot;

      Active := False;
      TableName := 'kecamatan';
      Filter := 'KODE_KECAMATAN = '+QuotedStr(kodekabkot+kodekec);
      Filtered := True;
      Active := True;
      First;
      cbkec.Items.Add(FieldByName('TAMPIL').AsString);
      cbkec.itemindex:=1;
      skec:=kodekabkot+kodekec;

      Active := False;
      TableName := 'desa';
      Filter := 'KODE_KECAMATAN = '+QuotedStr(kodekabkot+kodekec);
      Filtered := True;
      Active := True;
      First;
      while (not Eof) do
      begin
        cbkeldes.Items.Add(FieldByName('TAMPIL').AsString);
        Next;
      end;

    end;
  end
  else
  begin
    if(kodekabkot<>'00')then // petugas kabupaten
    begin
      with ztable1 do
      begin
        active:=false;
        tablename:='kabkot';
        filter:='KODE='+quotedstr(kodekabkot);
        filtered:=true;
        active:=true;
        first;
        cbkabkot.items.add(fieldbyname('TAMPIL').asstring);
        cbkabkot.itemindex:=1;
        cbkabkot.Enabled:=false;
        skabkot:=kodekabkot;

        Active := False;
      TableName := 'kecamatan';
      Filter := 'KODE_KABKOT = '+QuotedStr(kodekabkot);
      Filtered := True;
      Active := True;
      First;
        while (not Eof) do
        begin
          cbkec.Items.Add(FieldByName('TAMPIL').AsString);
          Next;
        end;
      end;
    end
    else  // petugas provinsi
    begin
      with ztable1 do
      begin
        active:=false;
        tablename:='kabkot';
        active:=true;
        first;
      end;
      while (not ZTable1.Eof) do
      begin
        cbKabkot.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
    end;
  end;

  pdfbutton.enabled:=true;

//  cbformat.Items.Add('Data Pemilih (A)');
//  cbformat.Items.Add('Daftar Pemilih Sementara (A1)');
//  cbformat.Items.Add('Daftar Pemilih Tambahan (A2)');
//  cbformat.Items.Add('Daftar Pemilih Tetap (A3)');
//  cbformat.Items.Add('Model A4');
//  cbformat.Items.Add('Model A5');
//  cbformat.Items.Add('Model A6');
//  cbformat.Items.Add('Model A7');
//  cbformat.ItemIndex:=0;



end;

procedure TTPS_Print_Form.JvThread1Execute(Sender: TObject;
  Params: Pointer);
var
  namadesa,namakec,namakot, namatps:string;
  dir:string;
  ext:ExternalFile;
begin
  CoInitialize(nil); // <-- manually call CoInitialize()
  namadesa:=t_namadesa;
  namakec:=t_namakec;
  namakot:=t_namakot;
  namatps:=t_namatps;
  dir:=t_dir;
  //showmessage(namadesa+namakec+namakot+namatps);
  while (not TTPS_Print_Form(params).ZQuery2.eof) do
          begin
            with TTPS_Print_Form(params).ztable1 do
            begin
              active:=false;
              tablename:='desa';
              filter:='KODE_DESA='+QuotedStr(TTPS_Print_Form(params).ZQuery2.FieldByName('KODE_DESA').AsString);
              filtered:=true;
              active:=true;
              first;
              namadesa:=FieldByName('TAMPIL').AsString;
              namakec:=FieldByName('KODE_KECAMATAN').AsString; // hanya sementara

              active:=false;
              tablename:='kecamatan';
              filter:='KODE_KECAMATAN='+QuotedStr(namakec);
              filtered:=true;
              active:=true;
              first;
              namakec:=FieldByName('TAMPIL').AsString;
              namakot:=FieldByName('KODE_KABKOT').AsString; // hanya sementara

              active:=false;
              tablename:='kabkot';
              filter:='KODE='+QuotedStr(namakot);
              filtered:=true;
              active:=true;
              first;
              namakot:=FieldByName('TAMPIL').AsString;
            end;
              printTPS(dir,'xls',choose_format, TTPS_Print_Form(params).ZQuery2.FieldByName('KODE_DESA').AsString, TTPS_Print_Form(params).ZQuery2.FieldByName('ID_TPS').AsString,
            namakot, namakec, namadesa, ZQuery2.FieldByName('NAMA').AsString);
            TTPS_Print_Form(params).ZQuery2.next;
          end;

  CoUnInitialize; //
  if JvThread1.Terminated then
  begin
     Exit;
  end;
end;

procedure TTPS_Print_Form.JvThread2Execute(Sender: TObject;
  Params: Pointer);
var
  namadesa,namakec,namakot, namatps:string;
  dir:string;
  ext:ExternalFile;
begin
  CoInitialize(nil); // <-- manually call CoInitialize()
  namadesa:=t_namadesa;
  namakec:=t_namakec;
  namakot:=t_namakot;
  namatps:=t_namatps;
  dir:=t_dir;
  //showmessage(namadesa+namakec+namakot);

  while (not TTPS_Print_Form(params).zquery2.eof) do
          begin
            with TTPS_Print_Form(params).ztable1 do
            begin
              active:=false;
              tablename:='desa';
              filter:='KODE_DESA='+QuotedStr(copy(TTPS_Print_Form(params).ZQuery2.FieldByName('NAMA').AsString,9,8));
              filtered:=true;
              active:=true;
              first;
              namadesa:=FieldByName('TAMPIL').AsString;
              namakec:=FieldByName('KODE_KECAMATAN').AsString; // hanya sementara

              active:=false;
              tablename:='kecamatan';
              filter:='KODE_KECAMATAN='+QuotedStr(namakec);
              filtered:=true;
              active:=true;
              first;
              namakec:=FieldByName('TAMPIL').AsString;
              namakot:=FieldByName('KODE_KABKOT').AsString; // hanya sementara

              active:=false;
              tablename:='kabkot';
              filter:='KODE='+QuotedStr(namakot);
              filtered:=true;
              active:=true;
              first;
              namakot:=FieldByName('TAMPIL').AsString;
            end;
            //showmessage('proses '+copy(ZQuery2.FieldByName('NAMA').AsString,9,8));
              printTPS(dir,'xls',choose_format, copy(TTPS_Print_Form(params).ZQuery2.FieldByName('NAMA').AsString,9,8), '',
            namakot, namakec, namadesa, '');
            TTPS_Print_Form(params).zquery2.Next;
          end;
             
  CoUnInitialize; //
  if JvThread2.Terminated then
  begin
     Exit;
  end;
end;

procedure TTPS_Print_Form.cbtpsSelect(Sender: TObject);
begin
  if((cbtps.ItemIndex=0)or(cbtps2.ItemIndex=0)) then
  begin
    printButton.Enabled:=false;
    pdfButton.Enabled:=false;
    xlsbutton.Enabled:=false;
  end
  else
  begin
    printButton.Enabled:=true;
    pdfButton.Enabled:=true;
    xlsbutton.Enabled:=true;
  end;

end;

end.
